package com.example.vv.model.chat;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.vv.R;
import com.example.vv.frameWork.entity.ChatEntity;
import com.example.vv.frameWork.mvp.MvpFragment;
import com.example.vv.frameWork.until.ToolUtils;
import com.example.vv.frameWork.until.UiUntil;
import com.example.vv.model.chat.base.adapter.ChatAdapter;
import com.example.vv.model.chat.base.contract.ChatContract;
import com.example.vv.model.chat.base.presenter.ChatPresenter;
import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ChatFragment extends MvpFragment<ChatContract.View, ChatPresenter> implements ChatContract.View {

    @BindView(R.id.chat_recycler)
    RecyclerView chatRecycler;
    @BindView(R.id.chat_smart)
    SmartRefreshLayout chatSmart;
    private ChatAdapter chatAdapter;
    private ArrayList<ChatEntity.BaseData> baseData;

    @Override
    public int initLayout() {
        return R.layout.fragment_chat;
    }

    @Override
    public ChatPresenter initPresenter() {
        return new ChatPresenter();
    }

    @Override
    public void initView() {
        chatRecycler.setLayoutManager(new LinearLayoutManager(getMContext()));
        chatSmart.setRefreshHeader(new ClassicsHeader(getMContext()).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.c_ffffff).setAccentColorId(R.color.c_ff3c88));
        chatSmart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                chatSmart.finishLoadMore(2000);
                presenter.load();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                presenter.reflush();
                chatSmart.finishRefresh(2000);

            }
        });
        chatSmart.autoRefresh();
    }

    @Override
    public void initData() {
        chatSmart.autoRefresh();
        chatAdapter = new ChatAdapter(getMContext());

        chatAdapter.setMyData(baseData);
        chatRecycler.setAdapter(chatAdapter);
    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleBar() {
        return null;
    }

    @Override
    public Context getMContext() {
        return getContext();
    }

    @Override
    public void setChatList(ChatEntity chatEntity) {
        List<ChatEntity.BaseData> baseData = chatEntity.getBaseData();
        if(presenter.isFlush){
            if(ToolUtils.isListNull(baseData)){
                UiUntil.listEmptyView(mActivity,chatAdapter,chatRecycler);
            }else{
                chatAdapter.setMyData(baseData);
                chatSmart.finishRefresh();
            }

        }else{
            if(ToolUtils.isListNull(baseData)){
                chatSmart.finishLoadMoreWithNoMoreData();
            }else{
                chatAdapter.addMyData(baseData);
                chatSmart.finishLoadMore();
            }
        }
    }
}
